//
//  TheRedemptionViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/7.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "TheRedemptionViewController.h"
#import "UIViewController+CBPopup.h"
#import "EOSAccountResult.h"
#import "EOSService.h"
@interface TheRedemptionViewController ()
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UILabel *cupLab;
@property (nonatomic,strong) UILabel *netLab;
@property (nonatomic,strong) UITextField *cupTF;
@property (nonatomic,strong) UITextField *netTF;
@property (nonatomic,strong)UIButton*cancelBtn;
@property (nonatomic,strong)UIButton*sureBtn;
@property (nonatomic,strong)EOSAccountDelegatedBandwidth*delegate;
@property (nonatomic,strong) EOSService *eosService;
@property (nonatomic,strong) Wallet *wallet;
@property (nonatomic,copy) void(^sureBlock)(BOOL success);
@end

@implementation TheRedemptionViewController
+ (void)alertController:(UIViewController*)controller infoDelegate:(EOSAccountDelegatedBandwidth*)delegate withWallet:(Wallet*)wallet sureBlock:(void(^)(BOOL success))sureBlock{

    TheRedemptionViewController *vc = [TheRedemptionViewController new];
    vc.view.frame = CGRectMake(0, 0, AdaptedWidth(330), AdaptedWidth(350));
    vc.view.backgroundColor = UIColor.whiteColor;
    vc.delegate = delegate;
    vc.wallet = wallet;
    vc.sureBlock = sureBlock;
   
    [controller cb_presentPopupViewController:vc animationType:CBPopupViewAnimationFade aligment:CBPopupViewAligmentCenter overlayDismissEnabled:NO
                             overlayDismissed:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    ViewRadius(self.view, AdaptedWidth(13));
    self.titleLab = [UILabel new];
    self.titleLab.textColor = UIColorHex(#333333);
    self.titleLab.font = kBoldFont(17);
    self.titleLab.text = XLocalized(@"赎回抵押");
    [self.view addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(AdaptedWidth(25));
    }];
    self.nameLab = [UILabel new];
    self.nameLab.textColor = UIColorHex(#B5B5B5);
    self.nameLab.font = kDefaultFont(13);
    [self.view addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(AdaptedWidth(20));
        make.left.mas_equalTo(AdaptedWidth(25));
    }];
    self.cupLab = [UILabel new];
    self.cupLab.textColor = UIColorHex(#333333);
    self.cupLab.font = kBoldFont(16);
    self.cupLab.text = XLocalized(@"计算");
    [self.view addSubview:self.cupLab];
    [self.cupLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLab.mas_bottom).offset(AdaptedWidth(30));
        make.left.mas_equalTo(AdaptedWidth(25));
    }];
    self.cupTF = [UITextField new];
    self.cupTF.backgroundColor = UIColorHex(#F7F7F7);
    self.cupTF.layer.cornerRadius = AdaptedWidth(10);
    self.cupTF.keyboardType = UIKeyboardTypeDecimalPad;
    UIView*leftV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
    self.cupTF.leftView= leftV1;
    self.cupTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.cupTF];
    [self.cupTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cupLab.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    
    self.netLab = [UILabel new];
    self.netLab.textColor = UIColorHex(#333333);
    self.netLab.font = kBoldFont(16);
    self.netLab.text = XLocalized(@"网络");
    [self.view addSubview:self.netLab];
    [self.netLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cupTF.mas_bottom).offset(AdaptedWidth(20));
        make.left.mas_equalTo(AdaptedWidth(25));
    }];
    self.netTF = [UITextField new];
    self.netTF.backgroundColor = UIColorHex(#F7F7F7);
    self.netTF.layer.cornerRadius = AdaptedWidth(10);
    self.netTF.keyboardType = UIKeyboardTypeDecimalPad;
    UIView*leftV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 1)];
    self.netTF.leftView= leftV;
    self.netTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.netTF];
    [self.netTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.netLab.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    kWeakSelf(self);
    self.cancelBtn = [UIButton new];
    [self.cancelBtn setTitle:XLocalized(@"取消") forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
    [self.cancelBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself cb_dismissPopupViewControllerAnimated:YES completion:^{
        }];
    }];
    [self.view addSubview:self.cancelBtn];

    self.sureBtn = [UIButton new];
    [self.sureBtn setTitle:XLocalized(@"确定") forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:UIColorHex(#FFCC03) forState:UIControlStateNormal];
    [self.sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
    }];
    [self.view addSubview:self.sureBtn];

    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.right.equalTo(self.sureBtn.mas_left);
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.height.width.equalTo(self.cancelBtn);
    }];
   
    UIView*line = [UIView new];
    line.backgroundColor = UIColorHex(#C7C7CC);
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.sureBtn.mas_top);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(CGFloatFromPixel(1));
    }];
    UIView*line2 = [UIView new];
    line2.backgroundColor = UIColorHex(#C7C7CC);
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.centerX.bottom.mas_equalTo(0);
        make.width.mas_equalTo(CGFloatFromPixel(1));
    }];
    
    [self.sureBtn addTapBlock:^(UIButton * _Nonnull btn) {
        if ([weakself.cupTF.text floatValue]<=0 && [weakself.netTF.text floatValue]<=0) {
            [weakself.view makeToast:XLocalized(@"无效的数量！")];
            return ;
        }
        if (SNCompare(weakself.cupTF.text.length?weakself.cupTF.text:@"0",[weakself.delegate.cpu_weight getDigital])==NSOrderedDescending) {
            [weakself.view makeToast:XLocalized(@"输入数量超过可赎回CPU数量！")];
            return;
        }
        if (SNCompare(weakself.netTF.text.length?weakself.netTF.text:@"0",[weakself.delegate.net_weight getDigital])==NSOrderedDescending) {
            [weakself.view makeToast:XLocalized(@"输入数量超过可赎回NET数量！")];
            return;
        }
        
        [XAlertTool alertTitle:XLocalized(@"确认赎回?") message:nil cancelAction:^{
            
        } sureAction:^{
            [WalletTool checkPrivateInputPasswordWith:weakself.wallet complete:^(BOOL result,NSString * _Nonnull privateKey) {
                if (result) {
                    weakself.eosService.toUser = weakself.delegate.to;
                    NSString* stake_cpu_quantity = [NSString stringWithFormat:@"%@ %@",[NSString stringWithFormat:@"%.*lf",kSymbolLength_EOS,[weakself.cupTF.text.length?weakself.cupTF.text:@"0" doubleValue]],kSymbolName_EOS];
                    NSString* stake_net_quantity = [NSString stringWithFormat:@"%@ %@",[NSString stringWithFormat:@"%.*lf",kSymbolLength_EOS,[weakself.netTF.text.length?weakself.netTF.text:@"0" doubleValue]],kSymbolName_EOS];
                    weakself.eosService.stake_cpu_quantity = stake_cpu_quantity;
                    weakself.eosService.stake_net_quantity = stake_net_quantity;
                    weakself.eosService.account = kContractName_EOSIO;
                    weakself.eosService.available_keys = @[VALIDATE_STRING(weakself.wallet.wallet_public_key)];
                    weakself.eosService.actor = weakself.wallet.wallet_name;
                    weakself.eosService.privateKey = privateKey;
                    [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
                        weakself.eosService.action = ContractAction_UNDELEGATEBW;
                        [weakself.eosService redemptionResourcesComplete:^(BOOL success, NSString * _Nonnull errorMsg) {
                            [MBProgressHUD hideHUDForView:weakself.view];
                            [weakself.view makeToast:errorMsg];
                            if (success) {
                                !weakself.sureBlock?:weakself.sureBlock(success);
                                [weakself cb_dismissPopupViewControllerAnimated:YES completion:nil];
                            }
                        }];
                }
            }];
        }];
    }];
}

- (void)setDelegate:(EOSAccountDelegatedBandwidth *)delegate{
    _delegate = delegate;
    self.nameLab.text = [NSString stringWithFormat:@"%@:%@",XLocalized(@"赎回账号"),self.delegate.to];
    self.cupTF.text = [delegate.cpu_weight getDigital];
    self.netTF.text = [delegate.net_weight getDigital];
}

- (EOSService*)eosService{
    if (!_eosService) {
        _eosService = [EOSService new];
    }
    return _eosService;
}
#pragma mark - Navigation
/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
